<script type="text/javascript" src="{$site_url}/includes/libraries/javascript/jquery.plugins/jquery.validate.js"></script>
<!-- <script charset="utf-8" type="text/javascript" src="{$site_url}/includes/libraries/javascript/jquery.plugins/jquery.metadata.js"></script> -->
<script type="text/javascript" src="{$site_url}/includes/libraries/javascript/jquery.plugins/additional-methods.js"></script>
<script type="text/javascript" src="{$site_url}/includes/libraries/javascript/mlselection.js"></script>
<script type="text/javascript">
$.validator.addMethod('checkZipcode', function(value) {
    var region_id = $('#region_id').val();
    return checkPostcode(value, region_id);
});

$.validator.addMethod('checkContact', function(value) {
    var v0 = $('#phone_section').val(),
        v1 = $('#phone_code').val(),
        v2 = $('#phone_ext').val();
    var go = true;
    if (value == '') {
        if (v0 == '' && v1 == '' && v2 == '') {
            go = false;
        }
    }
    return go;
}, '手机号码和电话号码不能同时为空');

$.validator.addMethod('isAreaEmpty', function() {
    var v0 = $('#phone_section').val(),
        v1 = $('#phone_code').val();
    return checkArea(v0, v1);
}, '区号不正确');

$.validator.addMethod('isTelEmpty', function() {
    var v0 = $('#phone_section').val(),
        v1 = $('#phone_code').val();
    return checkTel(v0, v1);
}, '电话号码不正确');

function checkPostcode(val, region_id) {
    if (val == '' || region_id == '3409') {
        return true; //如果是海外，邮政编码可以为空
    } else {
        var _reg = /^\d{6}$/;
        return _reg.test(val);
    }
}

function checkArea(v0, v1) {
    var phone_mob = $('#phone_mob').val();
    var go = true;
    var r0 = /^\d{3,4}$/;
    if (phone_mob != '') {
        if(v1 == ''){
            if(v0 == ''){
                go = true;
            }else if(v0 != '' && r0.test(v0)){
                go = true;
            }else{
                go=false;
            }
        }else{
            if(r0.test(v0)){
                go=true;
            }else{
                go=false;
            }
        }
    }else{
      if (r0.test(v0)) {
          go = true;
      } else {
          go = false;
      }
    }
    return go;
}

function checkTel(v0, v1) {
    var phone_mob = $('#phone_mob').val();
    var go = true;
    var r1 = /^\d{7,8}$/;
    if (phone_mob != '') {
        if(v0 == ''){
            if(v1 == ''){
                go = true;
            }else if(v1 != '' && r1.test(v1)){
                go = true;
            }else{
                go=false;
            }
        }else{
            if(r1.test(v1)){
                go=true;
            }else{
                go=false;
            }
        }
    }else{
      if (r1.test(v1)) {
          go = true;
      } else {
          go = false;
      }
    }
    return go;
}

/*添加新地址*/
function address_hide(o) {
    $('#address_form input[type="reset"]').click();
    $(o).parent().hide();
    wl.hwloverlay();
}
/*运费详情*/
function carriage_s(o) {
    var p = $(o).position();
    $(o).siblings('.shop-m-adetail').css({
        'top': p.top + 20 + 'px',
        'left': p.left - 87 + 'px'
    }).show();
}
function carriage_h(o) {
    $(o).siblings('.shop-m-adetail').hide();
}
/*备注信息*/
function focusText(obj) {
    $(obj).unbind('input propertychange');
    var temp = '';
    $(obj).siblings('label').css('color', '#ccc');
    $(obj).bind('input propertychange', function() {
        var val = $(obj).val();
        if (val.length == 120) {
            temp = val;
        }
        // $(obj).siblings('div.tips').children('span').html(val.length);
        if (val.length > 120) {
            $(obj).val(temp);
            if ($('#wloverlay').length === 0) {
                wl.sConfirm({
                    'info': '留言字数超限',
                    'cancel': true
                });
            }
        }
        if (val.length > 0) {
            $(obj).siblings('label').hide();
        } else {
            $(obj).siblings('label').show();
        }
    });
    $(obj).css('height', '80px');
}
function blurText(obj) {
    var len = $(obj).val().length;
    $(obj).siblings('label').css('color', '#999');
    if (len < 22) {
        $(obj).css('height', '17px');
    }
}
/*发票*/
function show_invoice(obj) {
    $(obj).closest('table').next('div.invoice_wrap').toggle();
}
function empty_errorlabel() {
    if ($('.invoice_wrap label.error').length > 0) {
        $('.invoice_wrap label.error').remove();
    }
}
function empty_checkbox_error() {
    if ($('div.checkbox-errorlabel').length > 0) {
        $('div.checkbox-errorlabel').remove();
    }
}

$(function() {
    regionInit("region");

    /*添加新地址*/
    $('#shop_newaddr').bind('click', function() {
        var wrap = $('#eject_con');
        var w = $(window).width();
        wl.swloverlay();
        wrap.css('left', (w - 708) / 2 + 'px').show();
    });
    var shop_address_ul_lis = $('ul.shop-address-ul > li');
    if (shop_address_ul_lis.length < 1) {
        $('#shop_newaddr').click();
    }

    $('ul.shop-address-ul > li').live('click', function() {
        var c = $(this).children('input').attr('checked'),
            href = 'index.php?app=order',
            address_id = $(this).children('input').attr('id').replace('address_', '');
        if (c) {
            wl.sConfirm({
                'info': '更换地址后，您需要重新确认订单信息',
                'success': function() {
                    window.location.href = href + '&address_id=' + address_id;
                },
                'fail': function() {
                    window.location.reload();
                }
            });
        }
    });

    $('#address_form').validate({
        errorPlacement: function(error, element) {
            element.parent().append(error);
        },
        onkeyup: false,
        debug: true,
        submitHandler: function() {
            var consignee = $('input[name="consignee"]').val();
            var region_id = $('input[name="region_id"]').val();
            var region_name = $('input[name="region_name"]').val();
            var address = $('input[name="address"]').val();
            var zipcode = $('input[name="zipcode"]').val();
            var phone_mob = $('input[name="phone_mob"]').val();
            var phone_section = $('input[name="phone_section"]').val();
            var phone_code = $('input[name="phone_code"]').val();
            var phone_ext = $('input[name="phone_ext"]').val();
            var defaulti = $('#default').attr('checked') ? 1 : 0;
            $.post('index.php?app=order&act=ajaxAddAddress', {
                'consignee': consignee,
                'region_id': region_id,
                'region_name': region_name,
                'address': address,
                'zipcode': zipcode,
                'phone_mob': phone_mob,
                'phone_section': phone_section,
                'phone_code': phone_code,
                'phone_ext': phone_ext,
                'defaulti': defaulti
            }, function(data) {
                if (data.success) {
                    if (data.is_deafult == '1') {
                        var href = 'index.php?app=order';
                        window.location.href = href + '&address_id=' + data.address_id;
                    } else {
                        window.location.reload();
                    }
                } else {
                    window.alert('添加失败');
                }
            }, 'json');
        },
        rules: {
            consignee: {
                required: true,
                checkRealname: '收货人姓名由中英文组成'
            },
            address: {
                required: true
            },
            region_id: {
                countSelect: true
            },
            zipcode: {
                checkZipcode: true
            },
            phone_mob: {
                checkContact: true,
                isMobile: true
            },
            phone_section :{
                isAreaEmpty : true
            },
            phone_code: {
                isTelEmpty: true
            }
        },
        messages: {
            consignee: {
                required: '请如实填写收货人姓名.',
                checkRealname: '收货人姓名由中英文组成，1-20个字符.'
            },
            address: {
                required: '请填写详细地址.'
            },
            zipcode: {
                checkZipcode: '邮政编码错误.'
            }
        }
    });

    $('#confirm_order').bind('submit', function() {
        if ($('.invoice_wrap label.error').length > 0) {
            $('.invoice_wrap label.error').remove();
        }
        if ($('div.checkbox-errorlabel').length > 0) {
            $('div.checkbox-errorlabel').remove();
        }
        var inputs = $('input[placeholder="填写公司名称"]');
        var checkboxs = $('input[name="shipping_confirm"]');
        var len = 0;
        $.each(inputs, function() {
            if ($.trim($(this).val()).length <= 0 && $(this).prev('input').attr('checked') == true) {
                $(this).after('<label class="error">不能为空</label>');
                ++len;
            }
        });
        $.each(checkboxs, function() {
            if ($(this).attr('checked') != true) {
                $(this).parent('p').after('<div class="checkbox-errorlabel"><label class="error">请勾选确认运费到付</label></div>');
                ++len;
            }
        });
        if (len > 0) {
            return false;
        }
    });

});
</script>
<div class="eject_con" id="eject_con">
	<a class="eject_close" onclick="address_hide(this)">X</a>
	<div class="add address">
		<form action="#" method="post" id="address_form">
			<ul class="f-cb">
				<li>
					<h3>
						<i>*</i>
						收货人姓名:
					</h3>
					<p>
						<input type="text" class="text width_normal b-radius" name="consignee" maxlength="20" />
					</p>
				</li>
				<li>
					<h3> 
						<i>*</i>
						所在地区:
					</h3>
					<div  id="region">
						<input type="hidden" name="region_id" id="region_id" class="mls_id" />
						<input type="hidden" name="region_name" class="mls_names" />
						<select class="sel" name="sel">
							<option>{$lang.select_pls}</option>
							{html_options options=$regions}
						</select> <b class="field_message" style="font-weight:normal;"><label class="field_notice"></label></b> 
					</div>
				</li>
				<li>
					<h3>
						<i>*</i>
						详细地址:
					</h3>
					<p>
						<input type="text" class="text width_normal b-radius" name="address" placeholder="不必重复填写地区" maxlength="50" />
					</p>
				</li>
				<li>
					<h3>
						<i>&nbsp;</i>
						邮政编码:
					</h3>
					<div>
						<input type="text" class="text width_normal b-radius" name="zipcode" maxlength="6" onkeyup="this.value=this.value.replace(/\D/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/\D/g,''))" />
					</div>
				</li>
				<li>
					<h3>
						<i>&nbsp;</i>
						手机号码:
					</h3>
					<div>
						<input type="text" class="text width_normal b-radius" name="phone_mob" id="phone_mob" placeholder="电话号码、手机号码必须填一项" maxlength="11" onkeyup="this.value=this.value.replace(/\D/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/\D/g,''))" />
					</div>
				</li>
				<li>
					<h3>
						<i>&nbsp;</i>
						电话号码:
					</h3>
					<p>
						<input type="text" class="text width4 b-radius" name="phone_section" placeholder="区号" value="" maxlength="4" id="phone_section" onkeyup="this.value=this.value.replace(/\D/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/\D/g,''))" />
						-&nbsp;
						<input type="text" class="text w100 b-radius" name="phone_code" placeholder="电话号码" value="" id="phone_code" maxlength="8" onkeyup="this.value=this.value.replace(/\D/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/\D/g,''))" />
						-&nbsp;
						<input type="text" class="text width4 b-radius" name="phone_ext" placeholder="分机" value="" id="phone_ext" maxlength="6" onkeyup="this.value=this.value.replace(/\D/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/\D/g,''))" />
					</p>
				</li>
				<li>
					<h3>
						<i>&nbsp;</i>
						设为默认:
					</h3>
					<p>
						<input type="checkbox" class="check" name="default" id="default" value="1" />
						<label for="default">设置为默认地址</label>
					</p>
				</li>
			</ul>
			<div class="submit">
				<input type="hidden" name="type" value="1" />
				<input type="submit" class="btn b-radius" value="新增地址" id="address-hidbtn" />
				<input type="reset" class="btn b-radius"  value="重置" />
			</div>
		</form>
	</div>
</div>